#Agent group consists of:
#AilAg1 = login-ready voice
#AilAg2 = login-ready voice
#AilAg3 = login-ready Ag102 voice, email, chat, sms, fax
#AilAg4 = login-notready Ag103 email, chat, sms, fax
#AilAg5 = logout Ag104

#capacity rule - 10 emails

#tests 1 -13 use strategies that route incoming emails/chats into agent group
def email_1():
  """Customer sends email to agent. Agent receives and marks done"""
  emailMsg = Customer1.SendEmail(GetOption("ESAddr"), text = "Hello, AILAgent")
  recEmails = AilAg3.AcceptEmail([emailMsg])
  if recEmails:
    AilAg3.MarkDoneEmail(emailMsg)

def email_2():
  """Agent receives email and ignores it. Email is revoked and then delivered back to the same agent"""
  emailMsg = Customer1.SendEmail(GetOption("ESAddr"), text = "Hello, AILAgent")
  intr = AilAg3.IgnoreEmail(emailMsg)
  recEmails = AilAg3.AcceptEmail([emailMsg])
  if recEmails:
    AilAg3.MarkDoneEmail(emailMsg)


def email_3a():
  """Agent receives email and replies it"""
  emailMsg = Customer1.SendEmail(GetOption("ESAddr"), text = "Hello, AILAgent")
  recEmails = AilAg3.AcceptEmail([emailMsg])
  if recEmails:
    replyEmailIntr = AilAg3.ReplyEmail(emailMsg)
    AilAg3.MarkDoneEmail(emailMsg)
    Customer1.GetEmails([replyEmailIntr])

def email_3b():
  """Agent receives 2 emails and replies them"""
  emailMsg1 = Customer1.SendEmail(GetOption("ESAddr"), text = "Hello, AILAgent")
  emailMsg2 = Customer1.SendEmail(GetOption("ESAddr"), text = "Hello, AILAgent")
  recEmails = AilAg3.AcceptEmail([emailMsg1, emailMsg2])
  if recEmails:
    replyEmailIntr1 = AilAg3.ReplyEmail(emailMsg1)
    AilAg3.MarkDoneEmail(emailMsg1)
    replyEmailIntr2 = AilAg3.ReplyEmail(emailMsg2)
    AilAg3.MarkDoneEmail(emailMsg2)
    Customer1.GetEmails([replyEmailIntr1,replyEmailIntr2])


def email_4():
  """Agent3 receives email, transfers it to Agent4, Agent4 accepts it and marks done"""
  emailMsg = Customer1.SendEmail(GetOption("ESAddr"), text = "Hello, AILAgent")
  recEmails = AilAg3.AcceptEmail([emailMsg])
  if recEmails:
    try:
      AilAg4.SetReady()
      AilAg3.TransferEmailToAgent(emailMsg, AilAg4)
      recEmails = AilAg4.AcceptEmail([emailMsg])
      if recEmails:
        AilAg4.MarkDoneEmail(emailMsg)
    finally:
      AilAg4.SetNotReady()
      
def email_5():
  """Agent3 receives email, transfers it to Agent4, Agent4 ignores it, email is sent back to Agent3"""
  emailMsg = Customer1.SendEmail(GetOption("ESAddr"), text = "Hello, AILAgent")
  recEmails = AilAg3.AcceptEmail([emailMsg])
  if recEmails:

    AilAg3.TransferEmailToAgent(emailMsg, AilAg4)
    intr = AilAg4.IgnoreEmail(emailMsg)
    AilAg3.MarkDoneEmail(emailMsg) # email will come back without invitation


def email_6():
  """Agent3 receives email, transfers it logged out Agent5, email is sent back to Agent3"""
  emailMsg = Customer1.SendEmail(GetOption("ESAddr"), text = "Hello, AILAgent")
  recEmails = AilAg3.AcceptEmail([emailMsg])
  if recEmails:

    AilAg3.TransferEmailToAgent(emailMsg, AilAg5) #agent3 is not logged in
    AilAg3.MarkDoneEmail(emailMsg) # email will come back without invitation


      
def email_7():
  """Agent3 receives email, transfers it to the Place of Agent4, Agent4 accepts it and marks done"""
  emailMsg = Customer1.SendEmail(GetOption("ESAddr"), text = "Hello, AILAgent")
  recEmails = AilAg3.AcceptEmail([emailMsg])
  if recEmails:
    try:
      AilAg4.SetReady()
      AilAg3.TransferEmailToPlace(emailMsg, AilAg4)
      intr = AilAg4.AcceptEmail([emailMsg])
      if recEmails:
        AilAg4.MarkDoneEmail(emailMsg)
    finally:
      AilAg4.SetNotReady()      
      
      
def email_8():
  """Agent3 receives email, sets NotReady, transfers email to the InboundQueue, email is delivered to Agent4"""
  emailMsg = Customer1.SendEmail(GetOption("ESAddr"), text = "Hello, AILAgent")
  recEmails = AilAg3.AcceptEmail([emailMsg])
  if recEmails:
    try:
      AilAg4.SetReady()
      AilAg3.SetNotReady()        
      AilAg3.TransferEmailToQueue(emailMsg, GetOption("ESInboundQueue"))
      recEmails = AilAg4.AcceptEmail([emailMsg])
      if recEmails:
        AilAg4.MarkDoneEmail(emailMsg)
    finally:
      AilAg4.SetNotReady()  
      AilAg3.SetReady()

def email_9():
  """Agent3 receives email, transfers it to Agent4, Agent4 replies email"""
  emailMsg = Customer1.SendEmail(GetOption("ESAddr"), text = "Hello, AILAgent")
  recEmails = AilAg3.AcceptEmail([emailMsg])
  if recEmails:
    try:
      AilAg4.SetReady()
      AilAg3.TransferEmailToAgent(emailMsg, AilAg4)
      recEmails = AilAg4.AcceptEmail([emailMsg])
      if recEmails:
        replyEmailIntr = AilAg4.ReplyEmail(emailMsg)
        AilAg4.MarkDoneEmail(emailMsg)
        Customer1.GetEmails([replyEmailIntr])        
    finally:
      AilAg4.SetNotReady()
      
def email_10():
  """Customer sends 11 emails (capacity = 10), Agent3 processes 10 emails, then one more"""
  emailMsg1 = Customer1.SendEmail(GetOption("ESAddr"), subject = "1_", text = "Hello, AILAgent")
  emailMsg2 = Customer1.SendEmail(GetOption("ESAddr"), subject = "2_", text = "Hello, AILAgent")
  emailMsg3 = Customer1.SendEmail(GetOption("ESAddr"), subject = "3_", text = "Hello, AILAgent")
  emailMsg4 = Customer1.SendEmail(GetOption("ESAddr"), subject = "4_", text = "Hello, AILAgent")
  emailMsg5 = Customer1.SendEmail(GetOption("ESAddr"), subject = "5_", text = "Hello, AILAgent")
  emailMsg6 = Customer1.SendEmail(GetOption("ESAddr"), subject = "6_", text = "Hello, AILAgent")
  emailMsg7 = Customer1.SendEmail(GetOption("ESAddr"), subject = "7_", text = "Hello, AILAgent")
  emailMsg8 = Customer1.SendEmail(GetOption("ESAddr"), subject = "8_", text = "Hello, AILAgent")
  emailMsg9 = Customer1.SendEmail(GetOption("ESAddr"), subject = "9_", text = "Hello, AILAgent")
  emailMsg10 = Customer1.SendEmail(GetOption("ESAddr"), subject = "10_", text = "Hello, AILAgent")
  time.sleep(120)
  emailMsg11 = Customer1.SendEmail(GetOption("ESAddr"), subject = "11_", text = "Hello, AILAgent")
 
  recEmails = AilAg3.AcceptEmail([emailMsg1, emailMsg2, emailMsg3, emailMsg4, emailMsg5, \
                                  emailMsg6, emailMsg7, emailMsg8, emailMsg9, emailMsg10], timeout = 300)
  if recEmails:
    AilAg3.MarkDoneEmail(emailMsg1)
    AilAg3.MarkDoneEmail(emailMsg2)  
    AilAg3.MarkDoneEmail(emailMsg3)   
    AilAg3.MarkDoneEmail(emailMsg4)
    AilAg3.MarkDoneEmail(emailMsg5)  
    AilAg3.MarkDoneEmail(emailMsg6)    
    AilAg3.MarkDoneEmail(emailMsg7)
    AilAg3.MarkDoneEmail(emailMsg8)  
    AilAg3.MarkDoneEmail(emailMsg9)    
    AilAg3.MarkDoneEmail(emailMsg10)
  recEmails = AilAg3.AcceptEmail([emailMsg11])
  if recEmails:
    AilAg3.MarkDoneEmail(emailMsg11)  
 

def email_11():
  """Test of email functions"""
  emailMsg = Customer1.SendEmail(GetOption("ESAddr"), text = "Hello, AILAgent")
  recEmails = AilAg3.AcceptEmail([emailMsg])
  if recEmails:
    AilAg3.TestEmailFunctions(emailMsg)
    AilAg3.MarkDoneEmail(emailMsg)

    
def email_12():
  """Test of outbound email functions"""
  emailMsg = Customer1.SendEmail(GetOption("ESAddr"), text = "Hello, AILAgent")
  recEmails = AilAg3.AcceptEmail([emailMsg])
  if recEmails:
    reply = AilAg3.CreateReply(emailMsg)
    AilAg3.TestEmailOutFunctions(reply) #
    AilAg3.DeleteIntr(reply) 
    AilAg3.MarkDoneEmail(emailMsg)    


def email_13():
  """10 emails"""
  emailMsg1 = Customer1.SendEmail(GetOption("ESAddr"), subject = "1_", text = "Hello, AILAgent")
  emailMsg2 = Customer1.SendEmail(GetOption("ESAddr"), subject = "2_", text = "Hello, AILAgent")
  emailMsg3 = Customer1.SendEmail(GetOption("ESAddr"), subject = "3_", text = "Hello, AILAgent")
  emailMsg4 = Customer1.SendEmail(GetOption("ESAddr"), subject = "4_", text = "Hello, AILAgent")
  emailMsg5 = Customer1.SendEmail(GetOption("ESAddr"), subject = "5_", text = "Hello, AILAgent")
  emailMsg6 = Customer1.SendEmail(GetOption("ESAddr"), subject = "6_", text = "Hello, AILAgent")
  emailMsg7 = Customer1.SendEmail(GetOption("ESAddr"), subject = "7_", text = "Hello, AILAgent")
  emailMsg8 = Customer1.SendEmail(GetOption("ESAddr"), subject = "8_", text = "Hello, AILAgent")
  emailMsg9 = Customer1.SendEmail(GetOption("ESAddr"), subject = "9_", text = "Hello, AILAgent")
  emailMsg10 = Customer1.SendEmail(GetOption("ESAddr"), subject = "10_", text = "Hello, AILAgent")
 
  recEmails = AilAg3.AcceptEmail([emailMsg1, emailMsg2, emailMsg3, emailMsg4, emailMsg5, \
                                  emailMsg6, emailMsg7, emailMsg8, emailMsg9, emailMsg10], timeout =300)
 
  if recEmails:
    replyEmailIntr1 = AilAg3.ReplyEmail(emailMsg1)
    AilAg3.MarkDoneEmail(emailMsg1)
    replyEmailIntr2 = AilAg3.ReplyEmail(emailMsg2)
    AilAg3.MarkDoneEmail(emailMsg2)
    replyEmailIntr3 = AilAg3.ReplyEmail(emailMsg3)
    AilAg3.MarkDoneEmail(emailMsg3)
    replyEmailIntr4 = AilAg3.ReplyEmail(emailMsg4)
    AilAg3.MarkDoneEmail(emailMsg4)
    replyEmailIntr5 = AilAg3.ReplyEmail(emailMsg5)
    AilAg3.MarkDoneEmail(emailMsg5)
    replyEmailIntr6 = AilAg3.ReplyEmail(emailMsg6)
    AilAg3.MarkDoneEmail(emailMsg6)
    replyEmailIntr7 = AilAg3.ReplyEmail(emailMsg7)
    AilAg3.MarkDoneEmail(emailMsg7)
    replyEmailIntr8 = AilAg3.ReplyEmail(emailMsg8)
    AilAg3.MarkDoneEmail(emailMsg8)
    replyEmailIntr9 = AilAg3.ReplyEmail(emailMsg9)
    AilAg3.MarkDoneEmail(emailMsg9)
    replyEmailIntr10 = AilAg3.ReplyEmail(emailMsg10)
    AilAg3.MarkDoneEmail(emailMsg10)    
    Customer1.GetEmails([replyEmailIntr1,replyEmailIntr2, replyEmailIntr3, replyEmailIntr4, replyEmailIntr5,
                        replyEmailIntr6, replyEmailIntr7, replyEmailIntr8, replyEmailIntr9, replyEmailIntr10])

#next tests use other Inbound queue and strategies. Make sure to reconfigure.
#Strategy must place incoming email into workbin (draft)
def email_14():
  """Customer sends email, router places it in workbin. Agent pulls and marks done"""
  emailMsg = Customer1.SendEmail(GetOption("ESAddr"), text = "Hello, AILAgent")
  recEmail = AilAg4.PullFromWorkbin(emailMsg)
  if recEmail:
    AilAg4.MarkDoneEmail(emailMsg)

def email_15():
  """Customer sends email, router places it in workbin. Agent pulls and replys"""
  emailMsg = Customer1.SendEmail(GetOption("ESAddr"), text = "Hello, AILAgent")
  recEmail = AilAg4.PullFromWorkbin(emailMsg)
  if recEmail:
    replyEmailIntr = AilAg4.ReplyEmail(emailMsg)
    AilAg4.MarkDoneEmail(emailMsg)
    Customer1.GetEmails([replyEmailIntr])    

 